﻿#region Usings

using System.IO;
using System.IO.IsolatedStorage;

#endregion

namespace SilverBox.Extensions
{
    /// <summary>
    /// Isolated Storage Extensions
    /// </summary>
    public static class IsolatedStorageFileStreamExtensions
    {
        /// <summary>
        /// Read all text in the Isolated Storage File Stream
        /// </summary>
        /// <param name="file">Isolated Storage File from the Isolated Storage</param>
        /// <returns>All the text contained on the file</returns>
        public static string ReadAllText(this IsolatedStorageFileStream file)
        {
            if (file.CanRead)
            {
                var sr = new StreamReader(file);
                var text = sr.ReadToEnd();
                sr.Close();
                return text;
            }
            return null;
        }

        /// <summary>
        /// Write all text on the Isolated Storage File
        /// </summary>
        /// <param name="file">Isolated Storage File from the Isolated Storage</param>
        /// <param name="text">All text to write</param>
        public static void WriteAllText(this IsolatedStorageFileStream file, string text)
        {
            if (file.CanWrite)
            {
                var sw = new StreamWriter(file);
                sw.Write(text);
                sw.Close();
            }
        }
    }
}